import { getToken } from '@/utils/storage'
import { Message } from 'element-ui'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/layout'),
    redirect: '/dashboard',
    children: [
      { path: '/dashboard', component: () => import('@/views/dashboard') },
      { path: '/article', component: () => import('@/views/article') }
    ]
  },
  { path: '/login', component: () => import('@/views/login') }
]

const router = new VueRouter({
  routes
})
const whileList = ['/login']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    return next()
  }
  if (whileList.includes(to.path)) {
    return next()
  }
  next('/login')
  Message.error('请先登录')
})
export default router
